<?php
namespace app\crontab\controller;

class InsurancePlan extends Crontab 
{
    public function createPlan()
    {
        // 以强险为准
        $day = 15; // 创建15天内到期的
        $dateS = date('Y-m-d');
        $dateE = date('Y-m-d',time() + $day * 86400);
        $map = [
            'insurance_date_c' => ['BETWEEN',[$dateS,$dateE]]
        ];
        // 
        $rows = db('Vehicle')->where($map)->select();


        foreach ($rows as $v) {
            $year = date('Y',strtotime($v['insurance_date_c']));
            # 查看是否已经创建计划
            $map = [
                'vehicle_id' => $v['id'], // 哪辆车
                'year' => $year
            ];
            if(!db('InsurancePlan')->where($map)->find())
            {
                $data = [
                    'vehicle_id' => $v['id'], // 哪辆车
                    'year' => $year,
                    'deadline_c' => $v['insurance_date_c'],
                    'deadline_b' => $v['insurance_date_b'],
                    'plate_number' => $v['plate_number'],
                    'company' => $v['company'],
                ];
                db('InsurancePlan')->insert($data);
            }
        }
    }
    private function _createAd()
    {
        $tel = $this->_tel();
        $title = $this->_title();
        $data = [
            'pay_time' => date('Y-m-d'),
            'text' => $tel.'开通了 '.$title
        ];
        db('InsurancePlan')->insert($data);
    }
    private function _tel()
    {
        $tel = [
            '133','134','135','136','137','138','139','131','132','147',
            '152','153','155','156','157','158','159',
            '178','177',
            '182','185','186','187'
        ];
        $rand = rand(0,22);
        $tou = $tel[$rand];

        $weihao = sprintf("%04d",rand(0,9999));

        return $tou.'****'.$weihao;
    }
    
    private function _title()
    {
        $goods = db('Goods')->select();
        $goodsLength = count($goods) - 1;
        $rand = rand(0,$goodsLength);

        return $goods[$rand]['title'].' '.$goods[$rand]['subtitle'];
    }
}

